package com.neusoft.elm.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.service.impl.BusinessServiceImpl;

@WebServlet("/BusinessLoginServlet")
public class BusinessLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // 设置响应编码
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        
        try {
            // 获取请求参数
            String businessIdStr = request.getParameter("businessId");
            String password = request.getParameter("password");
            
            // 参数验证
            if (businessIdStr == null || businessIdStr.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new LoginResult(false, "商家账号不能为空")));
                return;
            }
            
            if (password == null || password.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new LoginResult(false, "密码不能为空")));
                return;
            }
            
            // 转换商家ID
            Integer businessId;
            try {
                businessId = Integer.valueOf(businessIdStr.trim());
            } catch (NumberFormatException e) {
                out.print(mapper.writeValueAsString(new LoginResult(false, "商家账号格式错误")));
                return;
            }
            
            // 调用业务层验证登录
            BusinessService businessService = new BusinessServiceImpl();
            Business business = businessService.getBusinessByIdByPass(businessId, password);
            
            if (business != null) {
                // 登录成功，将商家信息存入session
                HttpSession session = request.getSession();
                session.setAttribute("business", business);
                
                out.print(mapper.writeValueAsString(new LoginResult(true, "登录成功")));
            } else {
                out.print(mapper.writeValueAsString(new LoginResult(false, "商家账号或密码错误")));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            out.print(mapper.writeValueAsString(new LoginResult(false, "系统错误，请稍后重试")));
        } finally {
            out.close();
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    
    // 登录结果类
    public static class LoginResult {
        private boolean success;
        private String message;
        
        public LoginResult() {}
        
        public LoginResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public void setSuccess(boolean success) {
            this.success = success;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
}